package kjh820909.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.lang.management.MemoryUsage;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import kjh820909.dao.MemberDAO;
import kjh820909.model.MemberVO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UpdateMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JPasswordField pwd;
	private JTextField Uid;
	private JTextField name;
	private JButton okButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UpdateMember dialog = 
					new UpdateMember("0",0,null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	// public UpdateMember( ) {
	public UpdateMember( final String mno, final int r,
			final JTable table) {
		// 회원 정보 수정을 위해
		// ManageMember에서 전달한 mno을 받기 위해
		// UpdateMember의 기본 생성자 대신
		// mno를 매개변수로 하는 새로운 생성자 작성
		
		// 또한, 회원정보 수정후 ManageMember의 
		// JTable의 model도 변경해야 하므로
		// 수정해야 할 행row과
		// JTable 객체도 생성자 매개변수로 선언
		
		setTitle("회원수정");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		JLabel txtName = new JLabel("이름");
		
		JLabel txtUid = new JLabel("아이디");
		
		JLabel txtPwd = new JLabel("비밀번호");
		
		pwd = new JPasswordField();
		
		Uid = new JTextField();
		Uid.setEnabled(false);
		Uid.setColumns(10);
		
		name = new JTextField();
		name.setColumns(10);
		
		JLabel txtGender = new JLabel("성별");
		
		JRadioButton isMale = new JRadioButton("남자");
		
		JRadioButton isFemale = new JRadioButton("여자");
		isFemale.setSelected(true);
		{
			okButton = new JButton("수정완료");
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(36)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(txtName)
								.addComponent(txtUid)
								.addComponent(txtPwd))
							.addGap(37)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(name, GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
								.addComponent(Uid, GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
								.addComponent(pwd, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(txtGender)
							.addGap(56)
							.addComponent(isMale)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(isFemale, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)))
					.addGap(149))
				.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
					.addContainerGap(246, Short.MAX_VALUE)
					.addComponent(okButton)
					.addGap(237))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(20)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtName)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtUid)
						.addComponent(Uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtPwd)
						.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(31)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtGender)
						.addComponent(isMale)
						.addComponent(isFemale))
					.addGap(55)
					.addComponent(okButton)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 395, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(229, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 192, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(216, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton cancelButton = new JButton("수정취소");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
						// 수정취소 버튼 클릭시
						// 회원정보 수정 창 닫음
					}
				});
				
				JButton btnNewButton = new JButton("수정완료");
				btnNewButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 수정완료 버튼 클릭시
						// 수정할 값을 MemberVO에 저장
						MemberVO m = new MemberVO(mno, null, new String(pwd.getPassword()),
								name.getText(), null, null);
						// MemberDAO의 updateMember 메서드 호출후
						//
						int cnt =
								new MemberDAO().updateMember(m);
						// 회원정보가 제대로 수정된 경우
						// 메세지 창 출력
						if (cnt >0)
						{JOptionPane.showMessageDialog(null, "수정완료!!");
						// UpdateMember수정한 값을
						// ManageMember의 JTable에도 적용해야 함
						// 이름 값만 변경됨
						table.setValueAt(name.getText(), r, 2);
						table.repaint(); // 변경사항 저굥\
						
						dispose(); // 현재창 닫기
						}
					}
				});
				buttonPane.add(btnNewButton);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		MemberVO m = new MemberDAO()
				.selectOneMember(mno);
		// MemberVO의 값을 컴퍼넌트에 출력
		Uid.setText(m.getUserid());
		name.setText(m.getName());
		pwd.setText(m.getPwd());
		
		
	} // 생성자
}






